/*
 * H.264 encoding using the x264 library
 * Copyright (C) 2005  Mans Rullgard <mans@mansr.com>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "libavcodec/avcodec.h"
#include "dmai_venc.h"

static int h264enc_frame(AVCodecContext *avctx, uint8_t *buf,
                      int bufsize, void *data)
{
	dmai_venc_t *codec = (dmai_venc_t *)avctx->priv_data;
	AVFrame *frame = (AVFrame *)data;
	int ret;

	if ((ret = dmai_venc_encode (codec, frame->data[0], buf, bufsize)) <= 0)
		return -1;
	return ret;
}

static av_cold int h264enc_close(AVCodecContext *avctx)
{
    dmai_venc_t *codec = (dmai_venc_t *)avctx->priv_data;

    dmai_venc_delete(codec);

    return 0;
}

static av_cold int h264enc_init(AVCodecContext *avctx)
{
	dmai_venc_t *codec = (dmai_venc_t *)avctx->priv_data;

	strcpy (codec->codecName, "h264enc");
	strcpy (codec->engineName, "codecServer");

	codec->width = avctx->width;
	codec->height = avctx->height;
	codec->bitRate = avctx->bit_rate;
	codec->frameRate = 30000;

	if (dmai_venc_create (codec)) {
		dmai_venc_delete (codec);
		return -1;
	}

	return 0;
}

AVCodec h264_encoder = {
    .name           = "h264enc",
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = CODEC_ID_H264,
    .priv_data_size = sizeof(dmai_venc_t),
    .init           = h264enc_init,
    .encode         = h264enc_frame,
    .close          = h264enc_close,
    .capabilities   = CODEC_CAP_DELAY,
    .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE },
    .long_name      = NULL,
};
